<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="options1Active == index ? 'active' : 'none'"
            v-for="(item, index) in options1"
            :key="index"
            @click="options1Fn(index, item.min, item.max)"
            >{{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: item.isActive }"
            v-for="(item, index) in options2"
            :key="index"
            @click="options2Fn(index, item.type)"
            >{{ item.txt }}</span
          >
          <!-- <span>实物礼品</span>
          <span>虚拟礼品</span> -->
        </li>
      </ul>
      <List :arr="goodsListShow" />
      <div style="text-align: center">
        <div v-show="!isReachBottom" class="isLoading">
          <i class="iconfont icon-loading"></i> 数据加载中
        </div>
        <span v-show="isReachBottom">数据加载完毕</span>
      </div>
    </div>
  </div>
</template>                            
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { GoodsSearchAPI } from "../request/api";
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils";

export default {
  data() {
    return {
      did: 0,
      type: 0,
      min: 0,
      max: 0, //传入0直接返回所有商品无视后台逻辑。
      keyword: "",
      goodsList: [],
      goodsListShow: [],
      page: 1,
      size: 8,
      isLoading: false,
      isReachBottom: false,
      options1: [
        { txt: "全部", min: 0, max: 0 },
        { txt: "我还可以兑换", min: 0, max: 0 },
        { txt: "0-500个", min: 0, max: 500 },
        { txt: "500-1000个", min: 500, max: 1000 },
        { txt: "1000-1500个", min: 1000, max: 1500 },
        { txt: "1500-2500个", min: 1500, max: 2500 },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      options1Active: 0,
      options2: [
        { txt: "全部", type: 0, isActive: true },
        { txt: "实物礼品", type: 1, isActive: false },
        { txt: "虚拟礼品", type: 2, isActive: false },
      ],
    };
  },
  created() {
    this.keyword = this.$route.query.keyword;
    this.goodsSearch();
  },
  mounted() {
    // 监听滚动
    window.addEventListener("scroll", this.scrollFn);
  },
  beforeDestroy() {
    // 解决滚动事件在其他页面也发生
    window.removeEventListener("scroll", this.scrollFn);
  },
  components: {
    Crumb,
    List,
  },
  methods: {
    // 封装获取全部商品请求
    async goodsSearch() {
      let res = await GoodsSearchAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max, //传入0直接返回所有商品无视后台逻辑。
        keyword: this.keyword,
      });
      // console.log("全部商品", res);
      this.goodsList = res.data;
      this.goodsListShow = this.goodsList.filter((item, index) => index < 8);

      // 切换排序初始化数据
      this.page = 1;
      this.isReachBottom = false;
      if (this.goodsList.length == this.goodsListShow.length) {
        this.isReachBottom = true;
        // 加载完毕后面内容不需要执行
      }
    },
    options1Fn(idx, min, max) {
      this.options1Active = idx;
      this.min = min;
      this.max = max;
      this.goodsSearch();
    },
    options2Fn(idx, clickType) {
      // 排他
      this.options2.forEach((item) => (item.isActive = false));
      this.options2[idx].isActive = true;
      // 根据类型筛选
      this.type = clickType;
      this.goodsSearch();
    },
    // 滚动事件
    scrollFn() {
      // console.log("发生滚动了");
      // console.log(getScrollHeight(), getClientHeight(), getScrollTop());
      // 什么时候加载:滚动距离+可视距离>文档高度
      if (getScrollTop() + getClientHeight() >= getScrollHeight() - 300) {
        // 判断数据是否加载完毕
        if (this.goodsList.length == this.goodsListShow.length) {
          this.isReachBottom = true;
          // 加载完毕后面内容不需要执行
          return;
        }
        // console.log("加载下一页");
        // 方式一:直接添加8条数据
        // 方式二:
        // page:1 size:8 index0-7
        // page:2 size:8 index8-15
        // page:3 size:8 index16-23
        // for循环每页加载下标数据

        // 节流控制不断触发滚动事件
        if (this.isLoading == false) {
          this.isLoading = true;
          this.page++;

          // 定时器模拟数据加载
          setTimeout(() => {
            for (
              let i = (this.page - 1) * this.size;
              i < this.page * this.size;
              i++
            ) {
              this.goodsList[i] && this.goodsListShow.push(this.goodsList[i]);
              console.log(i, this.page, this.size);
            }
            this.isLoading = false;
          }, 1500);
        }
      }
    },
  },
  // 监听路径变化发送请求
  watch: {
    "$route.query.keyword": {
      handler(newVal, oldVal) {
        this.keyword = newVal;
        this.goodsSearch();
      },
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
  .isLoading {
    i {
      animation: rotate 2s linear infinite;
      display: inline-block;
    }
    @keyframes rotate {
      0% {
        transform: rotate(0);
      }
      100% {
        transform: rotate(360deg);
      }
    }
  }
}
</style>